package org.nowcoder.leetcode.greedy;

/**
 * Title  : 860. Lemonade Change
 * Source : https://leetcode.cn/problems/lemonade-change/description/
 * Author : XrazYang
 * Date   : 2024-2-3
 */

public class LeetCode_860 {
    public boolean lemonadeChange(int[] bills) {
        int profit = 0;
        int[] par = new int[3]; //5,10,20
        for (int bill : bills) {
            if (profit < bill - 5) {
                return false;
            } else {
                //有没有对应面值支付找零
                int change = bill - 5; // 0,5,15 三种情况
                if (change == 0) {
                    par[0] += 1;
                }
                if (change == 5) {
                    par[1] += 1;
                    if (par[0] > 0) {
                        par[0] -= 1;
                    } else {
                        return false;
                    }
                }
                if (change == 15) {
                    par[2] += 1;
                    if (par[1] > 0 && par[0] > 0) {
                        par[1] -= 1;
                        par[0] -= 1;
                    } else if (par[0] >= 3) {
                        par[0] -= 3;
                    } else {
                        return false;
                    }
                }
                profit += 5;
            }
        }
        return true;
    }
}
